import { useState } from "react"

const useSendMessage = (num = 5) => {
    const [count, setCount] = useState(num)
    const [flag, setFlag] = useState(false)
    const countDown = () => {
        setFlag(true)

        const timer = setInterval(() => {
            //setCount可以传递回调函数
            setCount(previous => {
                if (previous < 1) {
                    setFlag(false)
                    clearInterval(timer)

                    return num
                } else {
                    return previous - 1
                }
            })
        }, 1000)
    }
    return [count, flag, countDown]
}
export default useSendMessage